;(function(){
	var JsonCl = function(){
		this.jsonTree = function(pNode,data){
			var subNode = createObject([data],"none");
			subNode.style.display = "block";
			pNode.appendChild(subNode);
		}

		function createObject(data,show){
			if(data && typeof data === "object"){
				var ul = document.createElement("ul");
				ul.className ='json-obj';
				ul.style.display = show;
				var isArray = data instanceof Array;
				for(var key in data){
					var li = document.createElement("li");
					li.className = 'json-item';
					var name = isArray ?"":key;
					var els = createItem(name,data[key],show);
					for(var i=0;i<els.length;i++){
						li.appendChild(els[i]);
					}
					ul.appendChild(li);
				}
				return ul;
			} else {
				var valueSpan = document.createElement("span");
				valueSpan.className ='json-val t-'+typeof data;
				//valueSpan.classList.add('json-val');
				//valueSpan.classList.add('t-'+typeof data);
				valueSpan.innerHTML = data;
				return valueSpan;
			}
		}

		function createItem(name,val,show){
			var els = [];
			//箭头
			if(typeof val === "object"){
				var a = document.createElement("a");
				if(show==="block"){
					a.innerHTML = '-';
					a.className = "json-toggle collapsed";
				}else{
					a.innerHTML = '+';
					a.className = "json-toggle";
				}
				a.onclick = jsonToggle;
				//els.push(a);
			}
			//名称
			var nameA = document.createElement("a");
			nameA.className = "json-name";
			var span = document.createElement("span");
			if(val && typeof val === "object"){
				nameA.className = "json-name json-toggle";
				if(show==="block"){
					span.innerHTML = '-';
				}else{
					span.innerHTML = '+';
				}
			}
			nameA.appendChild(span);
			var nameText = document.createTextNode(name?name + " : ":"");
			nameA.appendChild(nameText);
			nameA.onclick = jsonToggle;
			els.push(nameA);
			//显示条数
			if(val instanceof Array){
				var len = val.length;
				//有几条
				var msg = document.createElement("span");
				msg.className ='json-msg';
				var textNode = document.createTextNode("[  "+len+" item" + (len===1?"":"s") +"  ]");
				msg.appendChild(textNode);
				els.push(msg);
			}else if(val instanceof Object){
				var len = 0;
				try{
					len = Object.keys(val).length;
				}catch(e){
					for(var key in val){
						len ++;
					}
				}
				var msg = document.createElement("span");
				msg.className ='json-msg';
				var textNode = document.createTextNode("  {  "+len+" item" + (len===1?"":"s") +"  }");
				msg.appendChild(textNode);
				els.push(msg);
			}
			var node = createObject(val,show);
			els.push(node);
			return els;
			//return li;
			
		}
		function jsonToggle(){
			if(this.nodeType === 1){
				var node;
				//if(this.tagName === "A"){
				//	node = this;
				//}else{
					var nodes = this.parentNode.childNodes;
					for(var i=0;i<nodes.length;i++){
						var n = nodes[i];
						if(n.nodeType === 1 && n.tagName === "A"){
							node = n;
							break;
						}
					}
				//}
				if(node){
					toggleNode(node);
				}
				
			}
			
			function toggleNode(a){
				var className = a.className;
				if(className.indexOf('json-toggle')<0){
					return;
				}
				var text = a.innerHTML;
				var nodes = a.parentNode.childNodes;
				for(var i=0;i<nodes.length;i++){
					var node = nodes[i];
					if(node.nodeType === 1 && node.tagName === "UL"){
						if(node.style.display === "none"){ //展开
							node.style.display = "block";
							a.childNodes[0].innerHTML = "-";
						}else{//关闭
							node.style.display = "none";
							a.childNodes[0].innerHTML = "+";
						}
					}
					
				}
			}
		}
	}
	window.JsonCl = JsonCl;
})();